简体   繁体   English

在 Python 中人工创建 memory 用法

[英]Artificially creating memory usage in Python

I'm trying to create a pure memory intensive script in Python for testing purposes but every script that I try also increases my cpu.我正在尝试在 Python 中创建一个纯 memory 密集脚本用于测试目的,但我尝试的每个脚本也会增加我的 cpu。 I've read this post and I also tried, among others:我已经阅读了这篇文章,并且我还尝试过,其中包括:

#!/usr/bin/python
from datetime import datetime
startTime = datetime.now()

l1 = [17]*900
l2=[]

j=0
while j<9000:
    l2=l1
    j=j+1
print "Finished in ", datetime.now() - startTime

in order to copy an array to another array but once again I had cpu variations as well.为了将一个数组复制到另一个数组,但我又一次有了 cpu 变化。

UPDATED So, how can I cause a standard cpu utilization (100% usage in one core), 45% of memory utilization and after a couple of minutes an increase of memory utilization to 90%?更新所以,我怎样才能导致标准的 cpu 利用率(一个内核中 100% 的利用率)、 45% 的 memory 利用率和几分钟后 memory 利用率增加到 90%?

You have a couple of misconceptions that I'll try to address.您有几个误解,我将尝试解决。

  • You have to use CPU to use memory.您必须使用 CPU 才能使用 memory。 There's no other way.没有别的办法。
  • Your copy of a list is only assigning a pointer.您的列表副本仅分配一个指针。 You're not moving memory.你没有移动 memory。

If you want to increase memory utilization, you need to keep adding data to your list:如果您想增加 memory 利用率,您需要不断将数据添加到您的列表中:

l = []
for i in range(0, 1024*1024):
    l.append("*" * 1024)

Or using something similar to your method,或者使用类似于你的方法的东西,

l = [17] * 1024

for i in range(0, 16):
   l = l + l  # doubles the list each time.

That will allocate the memory.这将分配 memory。 If you want to measure access to it in isolation, you'll want to loop over l modifying the values or summing them.如果您想单独衡量对它的访问,则需要l修改值或对它们求和。

sum(l)

or或者

for i in range(0, len(l)):
    l[i] += 1

In the end, your benchmark is going to be very simplistic (like doesn't address multiple cores accessing memory simultaneously, doesn't take into account processor caches, lookahead, random vs serial access, etc.) Using Python is also not optimal because you are not in full control of the memory allocation and garbage collection.最后,您的基准测试将非常简单(例如不解决同时访问 memory 的多个内核,不考虑处理器缓存、前瞻、随机与串行访问等)使用 Python 也不是最佳的,因为您无法完全控制 memory 分配和垃圾回收。

Proper memory benchmarking is a deep subject...正确的 memory 基准测试是一个深入的主题......

Edit:编辑:

This is what you are asking for, more or less:这或多或少是您所要求的:

from datetime import datetime
from datetime import timedelta

memory1 = "*" * 1024**3

start = datetime.now()

j = 0

while (datetime.now() - start) < timedelta(minutes=1):
    j += 1

memory2 = "*" * 1024**3

while (datetime.now() - start) < timedelta(minutes=2):
    j += 1

You can adjust memory1 and memory2 to get your 40% and 90% depending on your actual system size.您可以根据实际系统大小调整memory1memory2以获得 40% 和 90%。 The program will need to use the CPU while it allocates the string.程序在分配字符串需要使用 CPU。 It first has to request the memory from the kernel, but then has to fill it in with '*' , otherwise the memory will only be virtual.它首先必须从 kernel 请求 memory,然后必须用'*'填写,否则 memory 将只是虚拟的。 If you were writing this in C, you could just touch one byte in each 4k page.如果你在 C 中写这个,你可以在每个 4k 页面中触摸一个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM